package com.example.jiemian;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ceshi extends Fragment {

    //定义集合对象
    private ListView listView1;
    private ListView listView2;
    //定义固定列表内容
    private String[] title1= new String[]{"空气温度值", "土壤湿度值", "CO2", "光照强度值"};
    private String[] title2 = new String[]{"用户密码设置",};

    //定义数据适配器
    private MyAdapter1 myAdapter1;
    private MyAdapter2 myAdapter2;

    //创建并执行视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        //设置主界面对象
        View view = inflater.inflate(R.layout.ceshi_main, viewGroup, false);
        //根据界面获取控件
        listView1 = (ListView) ((View) view).findViewById(R.id.ceshi_main_listview1);
        listView2 = (ListView) ((View) view).findViewById(R.id.ceshi_main_listview2);
        //将数据添加到适配器中
        myAdapter1 = new MyAdapter1(title1);
        myAdapter2 = new MyAdapter2(title2);
        //给列表添加适配器
        listView1.setAdapter(myAdapter1);
        listView2.setAdapter(myAdapter2);
        ////给列表中每一行添加点击事件
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog1(position);
            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog2(position);
            }
        });
        return view;

    }

    //数据适配器，数据与控件进行绑定
    public class MyAdapter1 extends BaseAdapter {
        //接受外界传入的数据
        private String[] titles1;

        //通过构造函数，将外界的值与内部变量进行交互
        public MyAdapter1(String[] title) {
            this.titles1 = title;
        }

        //获取传入参数长度
        @Override
        public int getCount() {
            return this.titles1.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //获取并返回数据绑定后的视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //声明视图对象，获取列表当中的子项内容
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.ceshi_main_item, null);
            TextView textView = view.findViewById(R.id.ceshi_main_item_title);
            textView.setText(title1[position]);
            return view;
        }
    }

    public class MyAdapter2 extends BaseAdapter {
        //接受外界传入的数据
        private String[] titles2;

        //通过构造函数，将外界的值与内部变量进行交互
        public MyAdapter2(String[] title2) {
            this.titles2 = title2;
        }

        //获取传入参数长度
        @Override
        public int getCount() {
            return this.titles2.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //获取并返回数据绑定后的视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //声明视图对象，获取列表当中的子项内容
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.ceshi_main_item, null);
            TextView textView = view.findViewById(R.id.ceshi_main_item_title);
            textView.setText(titles2[position]);
            return view;
        }
    }

    //通过模板窗体
    public void dialog1(int position) {
        //声明一个对话框模式窗体
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //设置弹出窗体标题
        builder.setTitle("设置报警值"+title1[position]);
        //弹出窗体内的信息
        String str1="最小值";
        String str2="最大值";
        int int1=1;
        int int2=2;
        //设置窗体标签的详细说明
        switch (position){
            case 0:
                int1=111;
                int2=333;
                str1+="(度)";
                str2+="(度)";
                break;
            case 1:
                int1=222;
                int2=444;
                str1+="(百分比)";
                str2+="(百分比)";
                break;
            case 2:
                int1=222;
                int2=444;
                str1+="(百分比)";
                str2+="(百分比)";
                break;
            case 3:
                int1=222;
                int2=444;
                str1+="(lx)";
                str2+="(lx)";
                break;
        }
        //获取并设置模板对象
        View view= LayoutInflater.from( getActivity() ).inflate(R.layout.dialog,null);
        //根据对象获取模板当中的控件
        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);
        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview_title2);

        //给控件赋值
        textView1.setText(str1);
        textView2.setText((str2));
        editText1.setText(""+int1);
        editText2.setText(""+int2);

        //将设置控件加载到窗体中
        builder.setView(view);
        //选择弹出窗体的’确定‘所触发的事件
        builder.setPositiveButton("q确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr=editText1.getText().toString();
            }
        });
        //选择弹出窗体的取消按钮所触发事件
        builder.setNegativeButton("取消",null);
        //创建窗体并进行显示
        builder.create().show();

    }

    public void dialog2(int position) {
        //声明一个对话框模式窗体
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //设置弹出窗体标题
        builder.setTitle(""+title2[position]);
        //弹出窗体内的信息
        String str1="密码";
        String str2="确认密码";
        int int1=1;
        int int2=2;
        //设置窗体标签的详细说明
        switch (position){
            case 0:
                int1=111;
                int2=333;
                str1+="x";
                str2+="xxx";
                break;
        }
        //获取并设置模板对象
        View view= LayoutInflater.from( getActivity() ).inflate(R.layout.dialog,null);
        //根据对象获取模板当中的控件
        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);
        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview_title2);

        //给控件赋值
        textView1.setText(str1);
        textView2.setText((str2));
        editText1.setText(""+int1);
        editText2.setText(""+int2);

        //将设置控件加载到窗体中
        builder.setView(view);
        //选择弹出窗体的’确定‘所触发的事件
        builder.setPositiveButton("q确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr=editText1.getText().toString();
            }
        });
        //选择弹出窗体的取消按钮所触发事件
        builder.setNegativeButton("取消",null);
        //创建窗体并进行显示
        builder.create().show();

    }

}